<template>
  <Modal v-model="modal" :title="title">
    <div class="tag-modal">
      <Form :model="form" :label-width="200" ref="form">
        <FormItem label="直发费用" v-if="isFee">
          <InputNumber :max="999999" :min="1" v-model="form.fee"></InputNumber>
        </FormItem>
      </Form>
    </div>
    <Loader :status="status" @refresh="onSave"></Loader>
    <div slot="footer">
      <Button @click="modal = false" size="large" type="text">取消</Button>
      <Button :disabled="btnDisabled" @click="onSave" type="primary" size="large">保存</Button>
    </div>
  </Modal>
</template>
<script>
export default {
  props: {
    idList: {
      type: Array
    }
  },
  data() {
    return {
      modal: false,
      title: '',
      btnDisabled: false,
      form: {
        fee: 1
      },
      item: null,
      type: '',
      status: 'pass',
      isFee: false,
      status: 'pass'
    }
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields()
      this.form = {
        fee: 1
      }
      this.item = null
      this.flag = false
      this.btnDisabled = false
      this.title = ''
      this.isFee = false
    },
    updateForm(item) {
      this.resetForm()
      this.isFee = true
      this.openModal()
    },
    openModal() {
      this.modal = true
    },
    closeModal() {
      this.modal = false
    },
    onSave() {
      if (this.btnDisabled) return
      this.btnDisabled = true
      let text
      let url = 'order/direct_delivery/batchUpdate'
      let param = {
        ids: this.idList
      }
      if (this.isFee) {
        if (this.form.fee) {
          param.fee = this.form.fee
          text = '批量设置直发费用'
        } else {
          this.$Message.error('请设置直发费用')
          this.closeModal()
          return
        }
      }
      this.status = 'loading'
      this.$ajaxGet(url, param).then(data => {
        this.$Notice.success({
          title: `${text}成功`
        })
        this.$emit('complete')
        this.closeModal()
        this.status = 'pass'
        // this.btnDisabled = false
      }).catch(() => {
        this.status = 'pass'
        this.btnDisabled = false
      })
    }
  }
}
</script>